package com.newasia.server.common.checkHandler;

import com.newasia.server.common.exception.ValidateException;
import com.newasia.server.common.result.ManagementRetCode;
import com.newasia.server.common.tool.CheckObjectIsNullUtils;
import com.newasia.server.common.tool.SpringUtil;
import com.newasia.server.controller.form.UserForm;
import com.newasia.server.dal.entitys.User;
import com.newasia.server.dal.persistence.UserMapper;
import jodd.util.StringUtil;

/**
 * @author LiChong
 * @description 用户名重复检测
 * @date 2020/4/7
 */

public class UsernameExistCheckHandler extends CheckHandler {

    UserMapper userMapper = SpringUtil.getBean(UserMapper.class);

    @Override
    public void doHandler(UserForm form) {
        String username = form.getUsername();
        if(StringUtil.isEmpty(username)){
            throw new ValidateException(
                    ManagementRetCode.USERORPASSWORD_NOT_EXIST.getCode(),
                    ManagementRetCode.USERORPASSWORD_NOT_EXIST.getMessage());
        }
        //检测用户名是否存在
        User user = userMapper.selectByPrimaryKey(username);
        if (!CheckObjectIsNullUtils.checkObjIsNull(user)) {
            throw new ValidateException(
                    ManagementRetCode.USERNAME_ALREADY_EXISTS.getCode(),
                    ManagementRetCode.USERNAME_ALREADY_EXISTS.getMessage());
        }
        if(null != next) {
            next.doHandler(form);
        }
    }
}
